import {
  editPhotoAPI,
  editProfileAPI,
  getcodeAPI,
  getProfileAPI,
  getUserProfileAPI,
  loginFormType,
  userLoginAPI,
} from '@/api/user';
import { setToken } from '@/utils/storage';
import { AppDispatch } from '../index';
import { profileType1 } from '../reducer/user';
export const getcodeAction = (mobile: string) => {
  return async () => {
    const res = await getcodeAPI(mobile);
    console.log(res);
  };
};

export const loginAction = (data: loginFormType) => {
  return async (dispatch: AppDispatch) => {
    const res = await userLoginAPI(data);
    // 保存token
    setToken(res.data);
    dispatch({ type: 'user/login', payload: res.data });
  };
};

export const getUserProfileAction = () => {
  return async (dispatch: AppDispatch) => {
    const res = await getUserProfileAPI();
    dispatch({ type: 'user/userInfo', payload: res.data });
  };
};

// 5 封装异步action的代码 获取请求数据
export const getProfileAction = () => {
  return async (dispatch: AppDispatch) => {
    const res = await getProfileAPI();
    // 调用同步action
    dispatch({ type: 'user/profile', payload: res.data });
  };
};

//  封装异步action的代码 获取请求数据
export const editPhotoAction = (file: FormData) => {
  return async (dispatch: AppDispatch) => {
    const res = await editPhotoAPI(file);
    // 调用同步action
    dispatch({ type: 'user/profile', payload: res.data });
  };
};

export const updateProfileAction = (data: Partial<profileType1>) => {
  return async (dispacth: AppDispatch) => {
    const res = await editProfileAPI(data);
    console.log('res  ----->  ', res);
    dispacth({
      type: 'user/edit',
      payload: data,
    });
  };
};
